const express = require('express');
const app = express();
const server = require('http').Server(app);
const path = require('path');
const compression = require('compression');
const io = require('socket.io')(server);

const tool = require('./system/tool.js');

const port = 3000;
const system = require('./system/router.js');
const viditor = require('./system/data/apps/Viditor/viditor.js');

const options = {
    maxAge: 86400000 / 2,
}

app.use(express.static(path.join(__dirname, 'public'), options));

app.use(compression());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(system);

// 各个应用的路由
// 前端静态文件路径格式（相对路径）/application/:appName/文件名
app.use('/application/AppStore', express.static(path.join(__dirname, 'system/data/apps/AppStore/public')));
app.use(require('./system/data/apps/AppStore/appstore.js'));

app.use('/application/Viditor', express.static(path.join(__dirname, 'system/data/apps/Viditor/public')));
app.use(require('./system/data/apps/Viditor/viditor.js'));

app.use('/application/2048', express.static(path.join(__dirname, 'system/data/apps/2048/public')));


// 系统socket通信
system.setIO(io);
// Viditor socket通信
viditor.setIO(io);

server.listen(port, () => {
    console.log(`Server listening at:`);
    console.log(`\t http://localhost:${port}`);
    const ip = tool.getIPAddress('IPv4');
    for (let i = 0; i < ip.length; i++) {
        console.log(`\t http://${ip[i]}:${port}`);
    }
});
